import { Vec3 } from 'cc';
import { ecs } from 'db://app-game/libs/ecs/ECS';
import { WeaponTypeEnum } from '../../inventory/Inventory.type';
import { Joysitick } from '../../joysitick/Joysitick';
import { RoleModelComp } from '../model/RoleModelComp';
import { Role } from '../Role';
import { RoleAttributeEnum, RoleUpperBodyStateEnum } from '../Role.type';

/** 角色状态组件 */
@ecs.register('RoleControl')
export class RoleControlComp extends ecs.Comp {
  /** 移动摇杆 */
  moveJoysitick: Joysitick = null;
  /** 攻击摇杆 */
  attackJoysitick: Joysitick = null;

  reset(): void {
    this.moveJoysitick = null;
    this.attackJoysitick = null;
  }
}

/** 角色碰撞系统 */
@ecs.register('RoleControl')
export class RoleControlSystem extends ecs.ComblockSystem implements ecs.ISystemUpdate {
  filter(): ecs.IMatcher {
    return ecs.allOf(RoleControlComp, RoleModelComp);
  }

  update(role: Role, dt: number): void {
    const { attackJoysitick, moveJoysitick } = role.RoleControl;
    // 1.控制角色攻击
    const angle = this.controllerRoleAttack(role);

    // 2.控制角色移动
    this.controllerRoleMove(role, angle);

    // // 3.角色闲置
    // if (!attackJoysitick.JoysitickControl.active && !attackJoysitick.JoysitickControl.active) {
    //   role.roleIdle();
    // }
  }

  /** 控制角色攻击 */
  controllerRoleAttack(role: Role) {
    const { attackJoysitick } = role.RoleControl;

    // 1.判断角色当前状态是否在白名单中
    const stateWhitelist = [
      RoleUpperBodyStateEnum.Idle,
      RoleUpperBodyStateEnum.Walk,
      RoleUpperBodyStateEnum.Run,
    ];

    // 2.如果攻击摇杆激活，则使用攻击摇杆的方向
    const stateName = role.getCurrentAnimation();
    const isInStateWhitelist = stateWhitelist.includes(stateName);
    if (attackJoysitick.JoysitickControl.active) {
      let angle = attackJoysitick.JoysitickControl.input;
      // 2.1如果角色当前状态在白名单中，则播放攻击动作
      if (isInStateWhitelist) {
        role.roleAttack(true);
      }
      return angle;
    }

    // 3 攻击摇杆释放(取消攻击)
    if (role.RoleModelEquipment.weapon.type === WeaponTypeEnum.Bow) {
      // 3.1 判断角色当前状态是否在射击装备中
      const ShootState = [
        RoleUpperBodyStateEnum.Begain,
        RoleUpperBodyStateEnum.Storage,
        RoleUpperBodyStateEnum.Storagest,
        RoleUpperBodyStateEnum.Release,
        RoleUpperBodyStateEnum.Reload,
        RoleUpperBodyStateEnum.NoBullets,
      ];
      // 3.2 如果角色当前状态在射击状态中，则释放弓箭
      if (ShootState.includes(stateName)) {
        role.roleAttack(false);
      }
    }
    return null;
  }

  /** 摇杆控制角色移动 */
  controllerRoleMove(role: Role, angle?: Vec3) {
    const { moveJoysitick } = role.RoleControl;
    const { attributes } = role.RoleModel;
    const moveSpeed = attributes.get(RoleAttributeEnum.MoveSpeed).value;

    // 1.判断角色当前状态是否在白名单中
    const stateWhitelist = [
      RoleUpperBodyStateEnum.Idle,
      RoleUpperBodyStateEnum.Walk,
      RoleUpperBodyStateEnum.Run,
    ];

    // 2.判断角色当前状态是否在白名单中
    const stateName = role.getCurrentAnimation();
    if (!stateWhitelist.includes(stateName)) return;

    // 3.如果攻击摇杆激活，则使用攻击摇杆的方向
    if (!angle) {
      angle = moveJoysitick.JoysitickControl.input;
    }

    // 4判断角色移动速度
    let velocity = Vec3.ZERO;
    if (moveJoysitick.JoysitickControl.active) {
      if (moveJoysitick.JoysitickControl.stickLength > 40) {
        velocity = moveJoysitick.JoysitickControl.input.clone().multiplyScalar(moveSpeed * 2);
        role.roleRun(velocity, angle);
      } else {
        velocity = moveJoysitick.JoysitickControl.input.clone().multiplyScalar(moveSpeed);
        role.roleWalk(velocity, angle);
      }
    } else {
      role.roleIdle();
    }
  }
}
